package com.ruoyi.nlp.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Set;

public class RuleUtil {

	public static List<Map<String,Object>> rules = new ArrayList<Map<String,Object>>();
	static{
//		rules.add(createRule("船舶电气设备、观通设备","U665","Marine Electrical Equipment or (ship and (electron or communication or computer or antenna or radar or telephone or television or electrical equipment or motor )"));
//		rules.add(createRule("船用强电设备","U665.1","ship and electrical equipment"));
//		rules.add(createRule("船用电机","U665.11","ship and motor"));
//		rules.add(createRule("电站、电网","U665.12","Ship power station or (ship and transmission network)"));
//		rules.add(createRule("电力拖动、推动、自动控制","U665.13","ship electrical power system or (ship and power drives )"));
//		rules.add(createRule("配电、电器仪表","U665.14","ship and electric equipment"));
//		rules.add(createRule("信号灯","U665.16","ship and signal lights"));
//		rules.add(createRule("船舶消磁设备","U665.18","ship and degaussing apparatus"));
//		rules.add(createRule("观通设备、船用弱电","U665.2","ship and (electronic or communication or computer or antenna or radar or telephone or television)"));
//		rules.add(createRule("船用电话","U665.23","ship and telephone"));
//		rules.add(createRule("船用电视","U665.24","ship and television"));
//		rules.add(createRule("红外线通信","U665.25","ship and infrared ray and communication"));
//		rules.add(createRule("船用电子设备","U665.26","ship and electronic equipment"));
//		rules.add(createRule("船用雷达","U665.22","ship and radar"));
//		rules.add(createRule("船用电子计算机技术","U665.261","ship and computer"));
//		rules.add(createRule("船用超声波技术","U665.262","ship and ultrasonic"));
//		rules.add(createRule("导航设备、水声设备","U666","(ship and navigation) or underwater acoustic or sonar"));
//		rules.add(createRule("导航设备","U666.1","ship and navigation"));
//		rules.add(createRule("综合导航系统","U666.11","ship and integrated navigation"));
//		rules.add(createRule("惯性导航系统","U666.12","ship and inertial navigation"));
//		rules.add(createRule("稳定平台","U666.12+1","ship and stabilized platform"));
//		rules.add(createRule("加速度表","U666.12+2","ship and (accelerometer or accelerometer)"));
//		rules.add(createRule("导航计算机","U666.12+5","ship and navigation and computer"));
//		rules.add(createRule("天文导航系统","U666.13","ship and celestial navigation"));
//		rules.add(createRule("光学天文导航","U666.131","ship and optical and celestial navigation"));
//		rules.add(createRule("光电天文导航","U666.132","ship and photoelectric and celestial navigation"));
//		rules.add(createRule("射电天文导航","U666.133","ship and radio and celestial navigation"));
//		rules.add(createRule("卫星导航","U666.134","ship and satellite navigation"));
//		rules.add(createRule("气象导航","U666.135","ship and weather routing"));
//		rules.add(createRule("无线电导航系统","U666.14","ship and radio navigation"));
//		rules.add(createRule("航海仪表","U666.15","nautical device"));
//		rules.add(createRule("罗经","U666.151","ship and (compass or compass)"));
//		rules.add(createRule("计程仪","U666.152","ship and log"));
//		rules.add(createRule("自动操舵仪","U666.153","autopilot"));
//		rules.add(createRule("微光导航仪","U666.154","ship and low-light and navigation equipment"));
//		rules.add(createRule("回声测深器","U666.155","echo and bathometer"));
//		rules.add(createRule("测冰仪","U666.157","ice fathometer"));
//		rules.add(createRule("航海模拟器","U666.158","echo and bathometer"));
//		rules.add(createRule("回声测深器","U666.155","echo and bathometer"));
//		rules.add(createRule("回声测深器","U666.155","echo and bathometer"));
//		rules.add(createRule("陀螺仪","U666.12+3","gyroscope"));
		
		for(Map<String,Object> m:rules){
			Set<Entry<String, Object>> entrySet = m.entrySet();
			for(Entry<String, Object> e:entrySet){
				System.out.println(e);
			}
		}
		List<Map<String,String>> word_index = new ArrayList<Map<String,String>>();
		Map<String,String> map = new HashMap<String, String>();
		
		map = new HashMap<String, String>();
		map.put("name", "惯性导航系统");
		map.put("code", "U666.12");
		map.put("rule", "all:ship AND all:\"inertial navigation\"");
		word_index.add(map);
		
		map = new HashMap<String, String>();
		map.put("name", "稳定平台");
		map.put("code", "U666.12+1");
		map.put("rule", "all:ship AND all:\"stabilized platform\"");
		word_index.add(map);
		
		map = new HashMap<String, String>();
		map.put("name", "加速度表");
		map.put("code", "U666.12+2");
		map.put("rule", "all:ship AND (all:accelerometer OR all:accelerometer)");
		word_index.add(map);
		
		map = new HashMap<String, String>();
		map.put("name", "陀螺仪");
		map.put("code", "U666.12+3");
		map.put("rule", "all:gyroscope");
		word_index.add(map);
		
		map = new HashMap<String, String>();
		map.put("name", "导航计算机");
		map.put("code", "U666.12+5");
		map.put("rule", "all:ship AND all:navigation AND all:computer");
		word_index.add(map);
		
		map = new HashMap<String, String>();
		map.put("name", "天文导航系统");
		map.put("code", "U666.13");
		map.put("rule", "all:ship AND all:\"celestial navigation\"");
		word_index.add(map);
		
		map = new HashMap<String, String>();
		map.put("name", "光学天文导航");
		map.put("code", "U666.131");
		map.put("rule", "all:ship AND all:optical AND all:\"celestial navigation\"");
		word_index.add(map);
		
		map = new HashMap<String, String>();
		map.put("name", "光电天文导航");
		map.put("code", "U666.132");
		map.put("rule", "all:ship AND all:photoelectric AND all:\"celestial navigation\"");
		word_index.add(map);
		
		map = new HashMap<String, String>();
		map.put("name", "射电天文导航");
		map.put("code", "U666.133");
		map.put("rule", "all:ship AND all:radio AND all:\"celestial navigation\"");
		word_index.add(map);
		
		map = new HashMap<String, String>();
		map.put("name", "卫星导航");
		map.put("code", "U666.134");
		map.put("rule", "all:ship AND all:\"satellite navigation\"");
		word_index.add(map);
		
		map = new HashMap<String, String>();
		map.put("name", "气象导航");
		map.put("code", "U666.135");
		map.put("rule", "all:ship AND all:\"weather routing\"");
		word_index.add(map);
		
		map = new HashMap<String, String>();
		map.put("name", "动平衡机");
		map.put("code", "U666.16+1");
		map.put("rule", "all:ship AND all:stabilization");
		word_index.add(map);
		
		map = new HashMap<String, String>();
		map.put("name", "测试技术");
		map.put("code", "U666.16");
		map.put("rule", "all:ship AND all:test");
		word_index.add(map);
		
		map = new HashMap<String, String>();
		map.put("name", "其他航海仪表");
		map.put("code", "U666.159");
		map.put("rule", "all:\"nautical device\"");
		word_index.add(map);
	}
	
	public static Map<String,Object> createRule(String name,String code,String oldRule){
		Map<String,Object> rule = new HashMap<String, Object>();
		oldRule = oldRule.toLowerCase();
		rule.put("name", name);
		rule.put("code", code);
		rule.put("oldRule", oldRule);
		String newRule = oldRule;
		Map<String,Map<String,String>> word_index = new HashMap<String, Map<String,String>>();
		autoGeneralRule(oldRule, word_index);
		Set<String> keySet = word_index.keySet();
		List<String> keyList = new ArrayList<String>();
		for(String key:keySet){
			keyList.add(key);
		}
		Collections.sort(keyList, new Comparator<String>(){  
            public int compare(String o1, String o2) {  
            	int len1 = o1.length();
            	int len2 = o2.length();      
            	if (len1 - len2 > 0)
					return -1;
				else if (len1 - len2 == 0)
					return 0;
				else
					return 1;   
            }  
        });
		for(String key:keyList){
			newRule = newRule.replace(key, "*"+word_index.get(key).get("index"));
		}
		rule.put("newRule", newRule);
		rule.put("word", word_index);
		return rule;
	}
	
	public static void wordIndex(String index,String isAnd,String word,Map<String,Map<String,String>> word_index){
		
		Map<String,String> map = new HashMap<String, String>();
		map.put("index", index);
		map.put("isAnd", isAnd);
		word_index.put(word, map);
	}
	
	public static Map<String,String> getClassNameAndCode(Set<String> words){
		Map<String,String> result = new HashMap<String, String>();
		for(Map<String,Object> rule:rules){
			Map<String,Map<String,String>> ruleWords = ((Map<String,Map<String,String>>)rule.get("word"));
			Set<String> matchedWords = new HashSet<String>();
			String newRule = rule.get("newRule").toString();
			for(String w:words){
				Map<String, String> map = ruleWords.get(w);
				if(map != null){
					matchedWords.add(w);
					newRule = newRule.replace("*"+map.get("index"), w);
				}
			}
			Set<String> keySet = ruleWords.keySet();
			for(String key:keySet){
				if(!matchedWords.contains(key)){
					if("false".equals(ruleWords.get(key).get("isAnd"))){
						newRule = newRule.replace("*"+ruleWords.get(key).get("index"), key);
					}
				}
			}
			if(rule.get("oldRule").toString().equals(newRule)){
				result.put("name", rule.get("name").toString());
				result.put("code", rule.get("code").toString());
				break;
			}
		}
		return result;
	}
	
	public static void autoGeneralRule(String rule,Map<String,Map<String,String>> map){
		
		String[] rules = rule.trim().split("\\s+");
		StringBuffer word  = new StringBuffer();
		int index = 0;
		String beforeBoolean = null;
		for(int i=0;i<rules.length;i++){
			
			if("or".equals(rules[i]) || "and".equals(rules[i])){
				Map<String,String> index_boolean = new HashMap<String, String>();
				index_boolean.put("index", (index++) +"");
				if(word.toString().trim().startsWith("(")){
					index_boolean.put("isAnd", "and".equals(rules[i])?"true":"false");
				}else{
					if(beforeBoolean != null && "and".equals(beforeBoolean)){
						index_boolean.put("isAnd","true");
					}else{
						index_boolean.put("isAnd", "and".equals(rules[i])?"true":"false");
					}
				}
				map.put(word.toString().replaceAll("\\(", "")
						.replaceAll("\\)", "").trim().toLowerCase(), index_boolean);
				word  = new StringBuffer();
				beforeBoolean = rules[i];
			}else{
				word.append(rules[i]+" ");
				if(i == rules.length - 1){
					Map<String,String> index_boolean = new HashMap<String, String>();
					index_boolean.put("index", (index++) +"");
					if(beforeBoolean != null && "and".equals(beforeBoolean)){
						index_boolean.put("isAnd","true");
					}else{
						index_boolean.put("isAnd", "and".equals(rules[i])?"true":"false");
					}
					map.put(word.toString().replaceAll("\\(", "")
							.replaceAll("\\)", "").trim().toLowerCase(), index_boolean);
				}
			}
		}
	}
	
	public static void main(String[] args) {
		
	}
}
